package com.example.user.controller;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;

@RestController
@RequestMapping("test")
public class TestController {
    @GetMapping("")
    public String test() {
        return "hello user module";
    }

    @Autowired
    private JavaMailSender sender;

    @Value("${spring.mail.username}")
    String from;

    @GetMapping("send-mail")
    public String send(String to) throws MessagingException, UnsupportedEncodingException {
        MimeMessage message = sender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(message);
        helper.setFrom(new InternetAddress(from, "Admin"));
        helper.setSubject("hello");
        helper.setTo(to);
        helper.setText("how are u");

        sender.send(message);

        return "ok";
    }
}
